package com.bw.controller;

import com.alibaba.fastjson2.JSON;
import com.bw.domain.Role;
import com.bw.domain.TbMenu;
import com.bw.domain.User;
import com.bw.service.RoleService;
import com.bw.utils.MenuUtil;
import com.bw.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: zhaijiaqi
 * @Date: 2025/3/12 21:00
 * @Description:
 **/
@Controller
@RequestMapping("role")
public class RoleController {

    @Autowired
    RoleService roleService;

    @Autowired
    RedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping("checkroleName")
    public R checkroleName(String roleName){
       Role role= roleService.getrole(roleName);
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> menuids=null;
       if(role!=null){
           menuids = roleService.getmenuByroleId(role.getId());
           map.put("roleId",role.getId());
           map.put("menuids",menuids);

       }
        return role!=null?R.OK(map):R.ERROR(500,"名称不存在");

    }


    /**
     * 执行权限添加
     * @param role
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public R add(Role role){
       boolean b= roleService.add(role);
       return b?R.OK():R.ERROR(500,"新增失败");
    }



    /**
     * 新增角色
     * @return
     */
    @RequestMapping("toadd")
    public String toadd(HttpSession session, Model model){
        User user = (User) session.getAttribute("user");
        List<TbMenu> tbMenuList = (List<TbMenu>) redisTemplate.opsForValue().get("tbMenuList" + user.getId());
        List<TbMenu> tbMenuList1 = MenuUtil.treeMenus(tbMenuList, null);
        model.addAttribute("tbMenuList", JSON.toJSON(tbMenuList1));
        return "/role/add";
    }



    /**
     * 去往权限管理列表
     * @return
     */
    @RequestMapping("tolist")
    public String tolist(){
        return "/role/list";
    }

    /**
     * 执行角色管理
     * @param user
     * @param session
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public R list(User user, HttpSession session,Integer page,Integer limit){

        User user1 = (User) session.getAttribute("user");
        List<Role> roleList = (List<Role>) redisTemplate.opsForValue().get("roleList"+user1.getId());
        String authId="";
        String roleName="";
        for (Role role : roleList) {
            roleName+=","+role.getRoleName();
            authId+=","+role.getId();

        }

        if(!roleName.contains("超级管理员")){
            authId=   authId.substring(1);
            user.setAuthId(authId);
        }


        PageHelper.startPage(page,limit);
       List<User> userList= roleService.list(user);
        PageInfo<User> info = new PageInfo<>(userList);

        return R.OK(info);
    }


}
